﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace XLT_MobileTest
{
    public enum RequestDifficulty { AskForForeign, AskForNative };

    public class Settings
    {
        const string PASSPHRASE = "blowfishHaHaHa";

        private Form1 __root;

        private bool _random;
        private bool _cs;
        private bool _schar;

        private string _username;
        private string _password;

        private string _path;

        private RequestDifficulty _diff;

        public Settings(Form1 root) 
        {
            __root = root;
            _path = String.Format("{0}\\settings\\settings.cfg", Form1.GetApplicationPath());

            /*if (!File.Exists(_path))
                File.Create(_path);*/

            if (!Init())
            {
                _random = true;
                _cs = false;
                _schar = false;
                _diff = RequestDifficulty.AskForNative;
                _username = "";
                _password = "";
            }
        }

        public void SaveSettings()
        {
            try
            {
                StreamWriter tw = new StreamWriter(_path);
                tw.Flush();
                string encName = MahdiHelper.EncryptString(_username, PASSPHRASE);
                string encPwd = MahdiHelper.EncryptString(_password, encName);
                if (_username == "false" || _password == "false" || _password.Length == 0 || _username.Length == 0)
                {
                    encName = "false";
                    encPwd = "false";
                }
                tw.WriteLine(String.Format("{0};{1};{2};{3};{4};{5}", _cs, _schar, _random, (int)_diff, encName, encPwd));
                tw.Close();
                /*FileStream file = File.Open(_path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
                file.Close();*/
            }
            catch (Exception ex) { __root.ShowError("Configuration Error", "Die aktuellen Konfigurationen konnte nicht gespeichert werden. Dieses Problem beeinträchtigt den Programmablauf jedoch nicht.\nPfad: " + _path +"\n"+ex.Message); }
        }

        public bool Init()
        {
            try
            {
                TextReader tr = new StreamReader(_path);

                string line = null;
                if ((line = tr.ReadLine()) != null)
                {
                    string[] parts = line.Split(';');

                    if (parts.Length >= 6)
                    {
                        _cs = bool.Parse(parts[0]);
                        _schar = bool.Parse(parts[1]);
                        _random = bool.Parse(parts[2]);
                        _diff = (RequestDifficulty)Int32.Parse(parts[3]);

                        if (parts[4].Length > 0 && parts[4] != "false")
                            _username = MahdiHelper.DecryptString(parts[4], PASSPHRASE);
                        else
                            _username = "";
                        if (parts[5].Length > 0 && parts[5] != "false")
                            _password = MahdiHelper.DecryptString(parts[5], parts[4]);
                        else
                            _password = "";
                    }
                    else
                    {
                        tr.Close();
                        return false;
                    }
                }
                else
                {
                    tr.Close();
                    return false;
                }
                tr.Close();
                return true;
            }
            catch (Exception ex) { __root.ShowError("Init Fehler", "Die Konfigurationsdatei konnte nicht verarbeitet werden!\n"+ex.Message); return false; }
        }

        public bool RequestRandom { get { return _random; } set { _random = value; } }
        public bool RequestCaseSensitive { get { return _cs; } set { _cs = value; } }
        public bool RequestSpecialCharacters { get { return _schar; } set { _schar = value; } }
        public string Username { get { return _username; } set { _username = value; } }
        public string Password { get { return _password; } set { _password = value; } }
        public RequestDifficulty RequestDifficulty 
        { 
            get { return _diff; } 
            set 
            { 
                _diff = value;
                __root.RefreshRequest();
            } 
        }
    }
}
